package com.example._03vehicle.controller;

import com.example._03vehicle.base.response.JsonResult;
import com.example._03vehicle.mapper.UserMapper;
import com.example._03vehicle.pojo.dto.UserLoginParam;
import com.example._03vehicle.pojo.dto.UserRegParam;
import com.example._03vehicle.pojo.entity.User;
import com.example._03vehicle.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Api(tags = "01.用户管理模块")
@RestController
@Slf4j
@RequestMapping(value ="/v1/user" )
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @ApiOperation(value = "用户注册")
    @ApiOperationSupport(order=10)
    @ApiImplicitParam(name = "userRegParam", value = "用户注册参数", required = true, dataType = "UserRegParam")
    @PostMapping(value = "/reg")
    public JsonResult reg(@RequestBody @Validated UserRegParam userRegParam){
       log.debug(userRegParam+"userRegParam");
       String username=userRegParam.getUsername();
       Integer i=userMapper.selectByUsername(username);
       if(i>0){
           return JsonResult.success(i);
       }
       User user=new User();
       BeanUtils.copyProperties(userRegParam,user);
       log.debug(user+"user");
       user.setCreateTime(new Date());
       user.setUpdateTime(new Date());
       log.debug(user+"user");
       int num=userMapper.insertByUser(user);
       if(num>0){return JsonResult.success();}
       else{return JsonResult.fail();}
    }
    @ApiOperation(value = "用户登录")
    @ApiOperationSupport(order=20)
    @ApiImplicitParam(name = "userRegParam", value = "用户登录参数", required = true, dataType = "UserRegParam")
    @PostMapping(value = "/login")
    public JsonResult login(@RequestBody UserLoginParam userLoginParam, @ApiIgnore HttpSession session){
        log.debug(userLoginParam+"userRegParam");
        String username=userLoginParam.getUsername();
        String password=userLoginParam.getPassword();
        UserVO uesrVO=userMapper.selectByUsernameAndPassword(username,password);
        if(uesrVO!=null){
            //设置用户标识
            session.setAttribute("user",uesrVO);
            return JsonResult.success();
        }
        else{
            return JsonResult.user_password_error();
        }
    }
    @GetMapping("/getCurrentUser")
    @ApiOperation(value = "用户获取当前用户名")
    @ApiOperationSupport(order=30)
    public JsonResult getCurrentUser(@ApiIgnore HttpSession session){
        UserVO userv0= (UserVO)session.getAttribute("user");
        return JsonResult.success(userv0);}

    @GetMapping("logout")
    @ApiOperation(value = "用户退出登录")
    @ApiOperationSupport(order=40)
    public JsonResult logout(@ApiIgnore HttpSession session){
        session.removeAttribute("user");
        return JsonResult.success();
    }
    //删除用户功能
    @GetMapping("delete")
    @ApiOperationSupport(order=50)
    @ApiOperation(value = "删除用户功能")
    public JsonResult deleteUser( @ApiIgnore HttpSession session) {

        UserVO uservo = (UserVO) session.getAttribute("user");
        if (uservo == null) {
            return JsonResult.notlogin_error();
        }
        Long id = uservo.getId();
        int num = userMapper.deleteByPrimaryKey(id);
        if (num > 0) {
            return JsonResult.success(num);
        } else {
            return JsonResult.crimeNotice();
        }
    }
}
